#!/usr/bin/python

from xml.dom.minidom import parse
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("farmTestItems.txt")
collection = DOMTree.documentElement

# Get all the movies in the collection
items = collection.getElementsByTagName("item")

def xpCheck(tree):
    finalXP = "NA"
    try:
        buyXpValue = tier.getElementsByTagName("buyXp")[0].childNodes[0].data
    except:
        buyXpValue = "NA"
    try:
        plantXpValue = tier.getElementsByTagName("plantXp")[0].childNodes[0].data
    except:
        plantXpValue = "NA"
    try:
        XpValue = tier.getElementsByTagName("XP")[0].childNodes[0].data
    except:
        XpValue = "NA"
    for i in [buyXpValue,plantXpValue,XpValue]:
        if i != "NA":
            finalXP = i
        else:
            pass
    return finalXP

def tierCheck(tiers):
    for tier in tiers:
        if tier.getAttribute("id") == "1":
            if tier.getAttribute("market") == "cash":
                cashValue = tier.getElementsByTagName("cash")[0].childNodes[0].data
                xpValue = xpCheck(tier)
                return(cashValue,"NA",xpValue)
            elif tier.getAttribute("market") == "coins":
                coinValue = tier.getElementsByTagName("cost")[0].childNodes[0].data
                xpValue = xpCheck(tier)
                return("NA",coinValue,xpValue)
            else:
                return("ERROR","ERROR","ERROR")
                       

writer = open("parsedXML.txt", "w")

writer.write("Name\tType\tBuyable\tPlaceable\tGiftable\tRequired Level\tCash Cost\tCoin Cost\t")
writer.write("XP Earned\tStorage Level\tStorage Capacity\tLimited Edition Start\tLimited Edition End\t")
writer.write("Winterizable\tMinimum Neighbors\tLimit\tMW Level Req\tMW Level Change Req\tHarvested for Coins\tExperiment Gate")


# Print detail of each movie.
for item in items:
    if item.hasAttribute("name"):
        writer.write("\n%s" % item.getAttribute("name"))
    else:
        writer.write("\nERROR")
    if item.hasAttribute("type"):
        writer.write("\t%s" % item.getAttribute("type"))
    else:
        writer.write("\tNA")
    if item.hasAttribute("buyable"):
        writer.write("\t%s" % item.getAttribute("buyable"))
    else:
        writer.write("\tNA")
    if item.hasAttribute("placeable"):
        writer.write("\t%s" % item.getAttribute("placeable"))
    else:
        writer.write("\tNA")
    if item.hasAttribute("giftable"):
        writer.write("\t%s" % item.getAttribute("giftable"))
    else:
        writer.write("\tNA")
    try:
        level = item.getElementsByTagName("requiredLevel")[0].childNodes[0].data
        writer.write("\t%s" % level)
    except:
        writer.write("\tNA")
    if item.hasAttribute("tiered"):
        CashCoinXp = tierCheck(item.getElementsByTagName("tier"))
        writer.write("\t%s\t%s\t%s" % CashCoinXp)
    elif item.hasAttribute("market"):
        if item.getAttribute("market") in ("coin","coins"):
            cost = item.getElementsByTagName("cost")[0].childNodes[0].data
            writer.write("\t%s\t%s" % ("NA",cost))
            xpValue = xpCheck(item)
            writer.write("\t%s" % xpValue)
        elif item.getAttribute("market") == "cash":
            cash = item.getElementsByTagName("cash")[0].childNodes[0].data
            writer.write("\t%s\t%s" % (cash,"NA"))
            xpValue = xpCheck(item)
            writer.write("\t%s" % xpValue)
        else:
            try:
                cost = item.getElementsByTagName("cost")[0].childNodes[0].data
            except:
                cost = "NA"
            xpValue = xpCheck(item)
            writer.write("\t%s\t%s\t%s" % ("NA",cost,xpValue))
    else:
        try:
            cost = item.getElementsByTagName("cost")[0].childNodes[0].data
        except:
            cost = "NA"
        xpValue = xpCheck(item)
        writer.write("\t%s\t%s\t%s" % ("NA",cost,xpValue))
    try:
        storage = item.getElementsByTagName("storageSize")[0].childNodes[0].data
        writer.write("\t%s" % storage)
    except:
        writer.write("\tNA")
    try:
        capacity = item.getElementsByTagName("capacity")[0].childNodes[0].data
        writer.write("\t%s" % capacity)
    except:
        writer.write("\tNA")
    try:
        limitedStart = item.getElementsByTagName("limitedStart")[0].childNodes[0].data
        writer.write("\t%s" % limitedStart)
    except:
        writer.write("\tNA")
    try:
        limitedEnd = item.getElementsByTagName("limitedEnd")[0].childNodes[0].data
        writer.write("\t%s" % limitedEnd)
    except:
        writer.write("\tNA")
    try:
        level = item.getElementsByTagName("altGraphic")[0].childNodes[0].data
        writer.write("\tYes")
    except:
        writer.write("\tNo")
    try:
        neighbors = item.getElementsByTagName("minNeighbors")[0].childNodes[0].data
        writer.write("\t%s" % neighbors)
    except:
        writer.write("\tNA")
    try:
        limit = item.getElementsByTagName("limit")[0].childNodes[0].data
        writer.write("\t%s" % limit)
    except:
        writer.write("\tNA")
    try:
        mafiaLevel = item.getElementsByTagName("requiredMafiaLevel")[0].childNodes[0].data
        writer.write("\t%s" % mafiaLevel)
    except:
        writer.write("\tNA")
    try:
        mafiaChange = item.getElementsByTagName("requiredMafiaLevelChange")[0].childNodes[0].data
        writer.write("\t%s" % mafiaChange)
    except:
        writer.write("\tNA")
    try:
        coinYield = item.getElementsByTagName("coinYield")[0].childNodes[0].data
        writer.write("\t%s" % coinYield)
    except:
        writer.write("\tNA")
    try:
        experimentGate = item.getElementsByTagName("experimentGate")[0].childNodes[0].data
        writer.write("\t%s" % experimentGate)
    except:
        writer.write("\tNA")


writer.close()
